#include <cstdio>
using namespace std;
int n,q;
int ans,noww;
int a[1001],b[1001],c[1001];
int main()
{
    //freopen("librarian.in","r",stdin);
    //freopen("librarian.out","w",stdout);
     scanf("%d%d",&n,&q);
     for(int i=1;i<=n;i++)
         scanf("%d",&a[i]);
     for(int i=1;i<=q;i++)
         scanf("%d%d",&b[i],&c[i]);
     for(int i=1;i<=q;i++)
    {
          ans=-1;noww=1;
          for(int j=1;j<=n;j++)
        {
            while(b[i]>0)
            {
                  noww*=10;
                  b[i]--;
            }
            if(a[j]%noww==c[i])
              if(ans==-1)
                  ans=a[j];
            else
            if(ans>a[j])
                ans=a[j];
          }
          printf("%d\n",ans);
      }
      return 0;
}

